package com.frogs.ucenter.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.UserLevelInfo;
import com.frogs.ucenter.service.UserLevelInfoService;

@Controller
@RequestMapping("/admin/userLevelInfo")
public class UserLevelInfoController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/userLevelInfo";

    private static final String HOME_VIEW = "admin/userLevelInfo/home";

    private static final String EDIT_VIEW = "admin/userLevelInfo/edit";

    @Autowired
    private UserLevelInfoService userLevelInfoService;

    @RequestMapping()
    public String listUserLevelInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createUserLevelInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",userLevelInfoService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveUserLevelInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserLevelInfo userLevelInfo) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        try {
            userLevelInfoService.insert( userLevelInfo ); 
            builder = DWZResponse.getSucessBuilder("success");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("fail" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showUserLevelInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( userLevelInfoService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateUserLevelInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserLevelInfo userLevelInfoNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                UserLevelInfo userLevelInfo = userLevelInfoService.getById( id ); 
                PropertyUtils.copyProperties(userLevelInfo, userLevelInfoNew);
                userLevelInfoService.update( userLevelInfo ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("success");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("fail" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteUserLevelInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            userLevelInfoService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("success");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("fail" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }

    private PageList<UserLevelInfo> getPageList(ModelMap model) {
        PageList<UserLevelInfo> userLevelInfoList = userLevelInfoService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("userLevelInfoList",userLevelInfoList);
        model.put("pageTurn",userLevelInfoList.getPageTurn());
        return userLevelInfoList;
    }
}